package CommitLink;

import Index.Index;
import gitobject.Commit;
import repository.Repository;
import zlib.ZLibUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @author shkstart
 * @create 2021-12-02 19:25
 */
public class CommitLink implements Serializable{
    private static final Long serialVersionUID = 3455710433843546891l;
    //commit链
    private LinkList commitList;
    //序列化时的文件路径
    private String commitPath = new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "commitLink";


    public CommitLink() throws IOException {
         commitList = new LinkList();
         if (readObject() != null){
             commitList = readObject().commitList;
         }

    }

    public LinkList getCommitList() {
        return commitList;
    }

    public void setCommitList(LinkList commitList) {
        this.commitList = commitList;
    }

    public void addCommitList(Commit commit){
        this.commitList.insertLast(commit);
    }

    public ArrayList<LinkList.LNode> getParent(Commit commit) throws Exception {
        return commitList.findByValue(commit.genKey()).getPre();
    }

//    public Commit getNext(Commit commit) throws Exception {
//        return commitList.findByValue(commit.genKey()).getNext().getCommit();
//    }

    /**
     * Todo: Serialize the object to the indexFile
     * @throws Exception
     */
    public void writeObject(){

        ObjectOutputStream oos = null;
        try{
            oos = new ObjectOutputStream(new FileOutputStream(commitPath));
            oos.writeObject(this);
            oos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(oos != null){
                try {
                    oos.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Todo: Serialize the object and compress with zlib.
     * @throws Exception
     */
    public void compressWrite(String temPath){
        /**
         * insertLast
 your code here.
         */
        ByteArrayOutputStream os = null;
        ObjectOutputStream oos = null;
        FileOutputStream f = null;
        try{
            /**
             * Todo: insertLast
 your code here.
             */
            os = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(os);
            f = new FileOutputStream(temPath);
            oos.writeObject(this);
            byte[] inputData = os.toByteArray();
            byte[] data = ZLibUtils.compress(inputData);
            ZLibUtils.compress(data, f);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(oos != null){
                try {
                    oos.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public CommitLink readObject(){

        ObjectInputStream ois = null;
        CommitLink commitLink = null;
        try{
            File file = new File(commitPath);
            if (file.length() == 0){

                return null;
            }

            ois = new ObjectInputStream(new FileInputStream(file));
            commitLink = (CommitLink) ois.readObject();
            return commitLink;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ois != null){
                try {
                    ois.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
